﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public partial class CTRoundBeamForm : System.Windows.Forms.Form
	{
		public CTRoundBeamForm()
		{
			this.InitializeComponent();
		}

		public CTRoundBeamForm(IList<XYZ> xyzs, double CTheight)
		{
			this.InitializeComponent();
			this.xyzs = xyzs;
			this.CTHeight = CTheight;
		}

		private bool IsEnoughHigh(string angleStr)
		{
			double num = Math.Abs(this.offSet);
			bool result = true;
			if (!(angleStr == "30"))
			{
				if (!(angleStr == "45"))
				{
					if (!(angleStr == "60"))
					{
						if (angleStr == "90")
						{
							if (num < 6.3 * this.CTHeight)
							{
								result = false;
							}
						}
					}
					else if (num < 3.5 * this.CTHeight)
					{
						result = false;
					}
				}
				else if (num < 2.0 * this.CTHeight)
				{
					result = false;
				}
			}
			else if (num < this.CTHeight)
			{
				result = false;
			}
			return result;
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.hytxtAngle.Enabled && Convert.ToDouble(this.hytxtAngle.Text.Trim()) < 0.1)
			{
				MessageBox.Show("偏移角度过小", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				return;
			}
			string text = "60";
			try
			{
				this.offSet = Convert.ToDouble(this.txtOffSet.Text.Trim());
				if (this.rdo30.Checked)
				{
					this.angle = 0.52359877559829882;
					text = "30";
				}
				else if (this.rdo45.Checked)
				{
					this.angle = 0.78539816339744828;
					text = "45";
				}
				else if (this.rdo60.Checked)
				{
					this.angle = 1.0471975511965976;
					text = "60";
				}
				else if (this.rdo90.Checked)
				{
					this.angle = Math.PI*.5;
					text = "90";
				}
				else if (this.rdoAny.Checked)
				{
					this.angle = Convert.ToDouble(this.hytxtAngle.Text.Trim()) * Math.PI / 180.0;
					text = this.hytxtAngle.Text.Trim();
				}
				if (!this.IsEnoughHigh(text))
				{
					MessageBox.Show("偏移高度过低，无法放下连接件！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
					return;
				}
				if (!ConnectHelper.CheckOffSet(this.xyzs, NumbericTools.mmToFeet(this.offSet), this.angle))
				{
					MessageBox.Show("偏移高度过大", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
					return;
				}
			}
			catch (Exception)
			{
				this.offSet = 0.0;
				this.angle = 1.0471975511965976;
			}
			this.userSetting.WriteValue("ctaroundbeam", "length", this.offSet.ToString());
			this.userSetting.WriteValue("ctaroundbeam", "angle", text);
			base.DialogResult = DialogResult.OK;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void DuctRoundBeamForm_Load(object sender, EventArgs e)
		{
			this.txtOffSet.Text = this.userSetting.GetValue("ctaroundbeam", "length");
			string value = this.userSetting.GetValue("ctaroundbeam", "angle");
			this.hytxtAngle.Text = value;
			if (value == "30")
			{
				this.rdo30.Checked = true;
				return;
			}
			if (value == "45")
			{
				this.rdo45.Checked = true;
				return;
			}
			if (value == "60")
			{
				this.rdo60.Checked = true;
				return;
			}
			if (!(value == "90"))
			{
				this.rdoAny.Checked = true;
				return;
			}
			this.rdo90.Checked = true;
		}

		private void rdoAny_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoAny.Checked)
			{
				this.hytxtAngle.Enabled = true;
				return;
			}
			this.hytxtAngle.Enabled = false;
		}

		private void txtOffSet_Validated(object sender, EventArgs e)
		{
			if (!new Regex("^[+-]?[1-9]+\\d*$").IsMatch(this.txtOffSet.Text))
			{
				MessageBox.Show("请输入正确的数字！");
				this.txtOffSet.Focus();
				this.txtOffSet.SelectAll();
				return;
			}
		}

		private UserSetting userSetting = new UserSetting();

		public double offSet;

		public double angle = 1.0471975511965976;

		private IList<XYZ> xyzs = new List<XYZ>();

		private double CTHeight;
	}
}
